package exam.axhu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import exam.axhu.entity.Choice;
import exam.axhu.service.ChoiceService;

@Service @Transactional
public class ChoiceServiceImpl implements ChoiceService{
//defaltBean: choiceServiceImpl
	@Resource SessionFactory factory;
	
	public int save(Choice choice) {
		factory.getCurrentSession().persist(choice);
		return 0;	 //成功保存用户
	}
	
	public void delete(int... choiceIDs) {
		for(int choiceID : choiceIDs){
			if(this.findByID(choiceID)!=null)
				factory.getCurrentSession().delete(factory.getCurrentSession().load(Choice.class,choiceID));
		}
		
	}

	public void update(Choice choice) {
		factory.getCurrentSession().merge(choice);
		
	}
	
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Choice findByID(int choiceID) {
		return (Choice) factory.getCurrentSession().get(Choice.class,choiceID);
        
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Choice> list() {
		return factory.getCurrentSession().createQuery("from Choice").list();
	}
	
	
}
